#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <hiredis/hiredis.h>
#include "global.h"
#include "logger.h"

static redisReply *                  s_reply;
static redisContext*                 s_pRedisCon;


int MyRedisConnect()
{
    struct timeval timeout = {2, 500000 };
    s_pRedisCon = redisConnectWithTimeout(g_htProfile.m_szRedisIp, atoi(g_htProfile.m_szRedisPort), timeout);

    s_reply = (redisReply*)redisCommand(s_pRedisCon,"AUTH %s",g_htProfile.m_szRedisAuth);
    if(!s_pRedisCon)
    {
        LOG__WARN("Connection error: can't allocate redis context");
        return -1;
    }

    if(s_pRedisCon->err)
    {
        LOG__WARN("Connection error: %s", s_pRedisCon->errstr);
        return -1;
    }

    return 0;
}


void redisSet(char* pCommand)
{
    
    redisReply* reply = ( redisReply*)redisCommand(s_pRedisCon,pCommand);   
    freeReplyObject(reply);
    
}


void DeinitRedis()
{   
    freeReplyObject(s_reply);
    redisFree(s_pRedisCon);
}


